﻿$(document).ready(function () {
    TranscationEntity.genBankInfoList();
    TranscationEntity.initEvent();
});
var addCount = 1;
var TranscationEntity = {
    tipElements: [],
    initEvent: function () {
        $(".btn-submit").on("click", TranscationEntity.checkAndPost);
        $(".btn-action").on("click", TranscationEntity.onAddTR);
        $(".btn-del").attr("onClick", "TranscationEntity.getDel(this)")
    },
    checkAndPost: function () {
        var authCode = $("#authCode").val();
        $.ajax({
            type: "POST",
            data: { authCode: authCode },
            url: "../api/authCodeCheck.ashx",
            cache: false,
            dataType: "json",
            success: function (retData) {
                if (retData == "0") {
                    TranscationEntity.postData();
                } else {
                    $("#authCode").val("");
                    bootbox.alert("验证码不正确!");
                }
            },
            error: function () {
                $("#authCode").val("");
                bootbox.alert("验证码不正确!");
            }
        });
    },

    postData: function () {
        var bankCardTypes = document.getElementsByName("bankCardType");
        var bankCardNos = document.getElementsByName("bankCardNo");
        var cardNames = document.getElementsByName("cardName");
        var amounts = document.getElementsByName("amount");

        var vbankCardTypes = [];
        var vbankCardNos = [];
        var vcardNames = [];
        var vamounts = [];

        for (var idx = 0; idx < bankCardTypes.length; idx++) {
            var value = bankCardTypes[idx].value;
            vbankCardTypes.push(value);
        }
        for (var idx = 0; idx < bankCardNos.length; idx++) {
            var value = bankCardNos[idx].value;
            if (value == null || "" == value) {
                bootbox.alert("卡号不能为空");
                return;
            } else {
                var reg = /^\d*$/;
                if (!reg.exec(value)) {
                    bootbox.alert("银行卡号必须全为数字");
                    return;
                }
            }

            vbankCardNos.push(value);
        }
        for (var idx = 0; idx < cardNames.length; idx++) {
            var value = cardNames[idx].value;
            if(value==null ||"" ==value){
                bootbox.alert("姓名不能为空!");
                return;
            }

            vcardNames.push(value);
        }
        for (var idx = 0; idx < amounts.length; idx++) {
            //需要填写逻辑代码
            var value = amounts[idx].value;
            if ("" == value) {
                bootbox.alert("金额不能为空");
                return
            } else {
                var d = /^\d+(\.\d{1,2})?$/;
                if (!d.test(value)) {
                    bootbox.alert("金额必须是整数,或者是带两位小数的数字");
                    return
                }
                if (parseFloat(value) == 0) {
                    bootbox.alert("金额必须大于0");
                    return
                }
            }
            vamounts.push(value);
        };
        var vPostData = vbankCardTypes.join(",") + "|" + vbankCardNos.join(",") + "|" + vcardNames.join(",") + "|" + vamounts.join(",");
        $("#postData").attr("value", vPostData);

        $("#formBank").submit();

       // console.trace(bankCardTypes);
       // console.trace(vbankCardTypes);
       // console.trace(vbankCardNos);
       // console.trace(vcardNames);
       // console.trace(vamounts);
       //// $(".btn-primary").attr("disabled", "true");
       // $.ajax({
       //     type: "POST",
       //     data: {
       //         cardType: vbankCardTypes.join(","),
       //         cardNo: vbankCardNos.join(","),
       //         cardName: vcardNames.join(","),
       //         //openBank: .join(","),
       //         amount: vamounts.join(",")
       //     },
       //     url: "/api/calcurate.ashx",
       //     cache:false,
       //     dataType: "json",
       //     success: function () {
       //         console.trace("success");
       //         $(".btn-primary").removeAttr("disabled");
       //         window.location.href = "/batchpay.aspx";
       //     },
       //     error: function (XMLHttpRequest, textStatus, errorThrown) {
               
       //             console.trace(XMLHttpRequest.statusText);
       //             console.trace(errorThrown);
       //         console.trace("fail");
       //     }
       // });
    },
    onAddTR: function () {
        addCount++;
        $("#dt_a tbody").append("<tr><td  class='td-bank'>    <select name='bankCardType' class='bank'></select></td><td class='td-name'>    <input type='text' name='cardName' maxlength='64' class='ipt ipt-sm'/></td> <td  class='td-card'>    <input type='text' name='bankCardNo' maxlength='19' class='ipt card-number' /></td><td class='td-money'>    <input type='text' name='amount' maxlength='12' class='ipt ipt-sm' /></td><td class='td-oper'>    <a class='btn btn-del' href='#'>删除</a></td></tr>");
        TranscationEntity.genBankInfoList();
        $(".btn-del").attr("onClick", "TranscationEntity.getDel(this)")
    },
    getDel: function (a) {
        if (addCount > 1) {
            $(a).parent().parent().remove();
            addCount--;
        }
    },
    genBankInfoList: function () {
        $.ajax({
            type: "get",
            url: "/api/listBankInfo.ashx",
            data: {
                enableRemit: "1"
            },
            cache: false,
            dataType: "json",
            success: function (c) {
                var b = c;
                $("[name='bankCardType']").remove("option");
                for (var a = 0; a < b.length; a++) {
                    $("[name='bankCardType']").append("<option value='" + b[a].BankName + "'>" + b[a].BankName + "</option>")
                }
            }
        });
    }
};

